package com.example.hetingting

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    private lateinit var db: AppDatabase
    private lateinit var historyDao: HistoryDao

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        db = AppDatabase.getDatabase(this)
        historyDao = db.historyDao()

        val number1EditText = findViewById<EditText>(R.id.number1)
        val number2EditText = findViewById<EditText>(R.id.number2)
        val resultTextView = findViewById<TextView>(R.id.result)

        val addButton = findViewById<Button>(R.id.addButton)
        val subtractButton = findViewById<Button>(R.id.subtractButton)
        val multiplyButton = findViewById<Button>(R.id.multiplyButton)
        val divideButton = findViewById<Button>(R.id.divideButton)

        addButton.setOnClickListener {
            val number1 = number1EditText.text.toString().toDoubleOrNull() ?: 0.0
            val number2 = number2EditText.text.toString().toDoubleOrNull() ?: 0.0
            val result = number1 + number2
            resultTextView.text = result.toString()
            saveHistory("$number1 + $number2 = $result")
        }

        subtractButton.setOnClickListener {
            val number1 = number1EditText.text.toString().toDoubleOrNull() ?: 0.0
            val number2 = number2EditText.text.toString().toDoubleOrNull() ?: 0.0
            val result = number1 - number2
            resultTextView.text = result.toString()
            saveHistory("$number1 - $number2 = $result")
        }

        multiplyButton.setOnClickListener {
            val number1 = number1EditText.text.toString().toDoubleOrNull() ?: 0.0
            val number2 = number2EditText.text.toString().toDoubleOrNull() ?: 0.0
            val result = number1 * number2
            resultTextView.text = result.toString()
            saveHistory("$number1 * $number2 = $result")
        }

        divideButton.setOnClickListener {
            val number1 = number1EditText.text.toString().toDoubleOrNull() ?: 0.0
            val number2 = number2EditText.text.toString().toDoubleOrNull() ?: 0.0
            val result = if (number2 != 0.0) number1 / number2 else "Cannot divide by zero"
            resultTextView.text = result.toString()
            saveHistory("$number1 / $number2 = $result")
        }
    }

    private fun saveHistory(operation: String) {
        CoroutineScope(Dispatchers.IO).launch {
            val history = History(operation = operation)
            historyDao.insert(history)
            Log.d("MainActivity", "History saved: $operation")
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.action_history -> {
                val intent = Intent(this, SecActivity::class.java)
                startActivity(intent)
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
}



